Imports MIMS.WRS.Entities
Imports MIMS.WRS.Business

Namespace UserControls

    Public Class ShippingLineDetail
        Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub
        Protected WithEvents ltrMessage As System.Web.UI.WebControls.Literal
        Protected WithEvents txtSLCode As System.Web.UI.WebControls.TextBox
        Protected WithEvents RequiredFieldValidator1 As System.Web.UI.WebControls.RequiredFieldValidator
        Protected WithEvents txtSLUrl As System.Web.UI.WebControls.TextBox
        Protected WithEvents Requiredfieldvalidator2 As System.Web.UI.WebControls.RequiredFieldValidator
        Protected WithEvents btnOK As System.Web.UI.WebControls.Button
        Protected WithEvents btnDelete As System.Web.UI.WebControls.Button
        Protected WithEvents btnCancel As System.Web.UI.WebControls.Button
        Protected WithEvents RegularExpressionValidator1 As System.Web.UI.WebControls.RegularExpressionValidator

        'NOTE: The following placeholder declaration is required by the Web Form Designer.
        'Do not delete or move it.
        Private designerPlaceholderDeclaration As System.Object

        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub

#End Region

        Protected mainObj As ShippingLine = Nothing

        Protected Property ReferrerUrl() As String
            Get
                Return Convert.ToString(ViewState("ReferrerUrl"))
            End Get
            Set(ByVal Value As String)
                ViewState("ReferrerUrl") = Value
            End Set
        End Property

        Protected ReadOnly Property IsAddNew() As Boolean
            Get
                Return RequestContext.ShippingLineID = 0
            End Get
        End Property

        Private Function BuildObject() As Boolean
            If IsAddNew Then
                mainObj = New ShippingLine
            Else
                mainObj = ShippingLineService.GetShippingLine(RequestContext.ShippingLineID)
                If mainObj Is Nothing Then
                    Utilities.ShowMessagePage("Failed to load requested shipping line. Shipping line may be deleted or not found")
                    Return False
                End If
            End If

            Return True
        End Function

        Private Sub UpdateData(ByVal bSave As Boolean)
            If (bSave) Then
                mainObj.Abbr = txtSLCode.Text
                mainObj.URL = txtSLUrl.Text
            Else
                txtSLCode.Text = mainObj.Abbr
                txtSLUrl.Text = mainObj.URL
            End If
        End Sub

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If RequestContext.CurrentUser.IS_ADMIN = False Then
                btnDelete.Visible = False
            End If

            If Not IsPostBack Then
                ReferrerUrl = Utilities.GetReferrerUrl("ShippingLineList.aspx")
                If IsAddNew Then
                    btnDelete.Visible = False
                    txtSLUrl.Text = "http://"
                Else
                    If (BuildObject()) Then
                        UpdateData(False)
                    End If
                    'txtSLCode.Enabled = False
                    btnDelete.Attributes("onclick") = "return confirm('Are you sure you want to delete?');"
                    btnDelete.Visible = True
                End If
            End If
        End Sub

        Private Function IsSLCodeExist() As Boolean
            Dim dummy As ShippingLine = ShippingLineService.GetShippingLineByCode(mainObj.Abbr)
            If Not (dummy Is Nothing) Then
                ltrMessage.Text = "Shipping Line has code already existed. Please enter another."
                Return True
            Else
                Return False
            End If
        End Function

        Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
            If (Page.IsValid) Then
                If (BuildObject()) Then
                    Dim oldSLCode As String = mainObj.Abbr
                    UpdateData(True)
                    If IsAddNew Then
                        If IsSLCodeExist() = True Then Return
                        ShippingLineService.CreateShippingLine(mainObj)
                    Else
                        If Not oldSLCode.ToLower().Trim().Equals(mainObj.Abbr.ToLower().Trim()) Then
                            If IsSLCodeExist() = True Then Return
                        End If
                        ShippingLineService.UpdateShippingLine(mainObj)
                    End If
                    Response.Redirect(ReferrerUrl)
                End If
            End If
        End Sub

        Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
            ShippingLineService.DeleteShippingLine(RequestContext.ShippingLineID)
            Response.Redirect(ReferrerUrl)
        End Sub

        Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
            Response.Redirect(ReferrerUrl)
        End Sub
    End Class

End Namespace